CREATE PROCEDURE [KS_ModelClone] 
@ModelName nvarchar(255),
@ModelTable nvarchar(255),
@ModelID int=0,
 @isrepeat int=1 output
As
  if exists(select top 1 * from ks_model where modelname=@modelname or modeltable=@modeltable)
   begin
    select @isrepeat=-2
   return null
   end
 else
   begin
  declare @MaxModelID  int
  declare @SQL nvarchar(4000)
  Select @MaxModelID=max(channelid)+1 FROM KS_Model
  Set @SQL='Insert Into [KS_Model]([channelid],[modeltype],[modelname],[modeltable],[modelitem],[modelitemunit],[modelstatus],[basictype],[modelfolder],[modelfsofolder],[fsolistnum],[intro],[chargesoption],[attributeoption],[receiptoption],[pkoption],[fieldsxml],[adminupfilestf],[adminupfilesdir],[userupfilestf],[userupfilesdir],[allowupfilessize],[allowupphototype],[allowupflashtype],[allowupmediatype],[allowuprealtype],[allowupothertype],[allowpublication],[publicationverify],[publicationrefreshhtmloption],[publicationmodifyverify],[commenttype],[commentlen],[newestday],[commenttemplate],[searchtemplate],[diggbyvisitor],[diggrepeat],[diggpertimes],[setting],[showoption],[seooption],[modelorderid]) 
    select '+convert(varchar(255),@MaxModelID)+',0,'''+convert(varchar(255),@ModelName)+''','''+convert(varchar(255),@ModelTable)+''',[modelitem],[modelitemunit],[modelstatus],[basictype],[modelfolder],[modelfsofolder],[fsolistnum],[intro],[chargesoption],[attributeoption],[receiptoption],[pkoption],[fieldsxml],[adminupfilestf],[adminupfilesdir],[userupfilestf],[userupfilesdir],[allowupfilessize],[allowupphototype],[allowupflashtype],[allowupmediatype],[allowuprealtype],[allowupothertype],[allowpublication],[publicationverify],[publicationrefreshhtmloption],[publicationmodifyverify],[commenttype],[commentlen],[newestday],[commenttemplate],[searchtemplate],[diggbyvisitor],[diggrepeat],[diggpertimes],[setting],[showoption],[seooption],modelorderid+1 from KS_Model where channelid='+convert(varchar(255),@ModelID)
   Exec (@SQL)
   Declare @Table nvarchar(4000)
   Select @Table=ModelTable From KS_Model Where channelid=@ModelID
   Set @SQL='select * into '+Convert(varchar(255),@modeltable)+' from '+@Table+' where 1=0'
   Exec (@SQL)
   Set @SQL=' alter table '+CONVERT(Nvarchar(255),@ModelTable)+' add constraint pk_'+convert(varchar(255),@MaxModelID)+'_infoid primary key(infoid)'
   Exec (@SQL)
   select @isrepeat=0
 end